/*
 * @lc app=leetcode.cn id=1288 lang=cpp
 *
 * [1288] 删除被覆盖区间
 */

// @lc code=start
class Solution {
public:
    int removeCoveredIntervals(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), [](vector<int> a, vector<int> b){
            if (a[0] == b[0]) {
                return a[1] > b[1];
            }
            return a[0] < b[0];
        });
        int left = intervals[0][0];
        int right = intervals[0][1];

        int res = 0;
        for (int i = 1; i < intervals.size(); ++i) {
            vector<int> cur = intervals[i];
            if (cur[0] >= left && cur[1] <= right) {
                res++;
            }
            if (cur[0] > right) {
                left = cur[0];
                right = cur[1];
            }
            if (cur[1] >= right && cur[0] <= right) {
                right = cur[1];
            }
        }
        return intervals.size() - res;
    }
};
// @lc code=end

